package com.base.cn.platform.os.controller.web.course.play.record;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.OrderStatus;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.catalog.CourseCatalog;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogService;
import com.base.cn.platform.os.service.course.cou.record.CourseLearnFlowService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.exam.record.LoadStartExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程播放记录
 *
 * @author SK
 * @since 2018-06-14
 **/
@Controller
@RequestMapping("/web/uc/course/play/record")
public class WebCoursePlayRecordController extends WebBaseController {

    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private CourseLearnFlowService courseLearnFlowService;
    @Autowired
    private LoadStartExamService loadStartExamService;
    @Autowired
    private CourseCatalogService courseCatalogService;

    /**
     * 保存课程播放记录
     *
     * @param playRecord 课程播放记录
     */
    @RequestMapping("/savePlayRecord")
    @ResponseBody
    public Map<String, Object> savePlayRecord(HttpServletRequest request,
                                              @RequestBody Map<String, Object> playRecord) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        playRecord.put("userId", userId);
        playRecord.put("source", 1);
        return coursePlayRecordService.saveCoursePlayRecord(playRecord);
    }

    /**
     * 用户学习记录
     *
     * @return
     */
    @RequestMapping("/userStudyRecord")
    public String userStudyRecord() {
        return resultPage("/course/play/record/front-studyRecord");
    }

    /**
     * 加载学习记录
     *
     * @param request
     * @param params      查询条件
     * @param currentPage 页码
     * @return 操作页面
     */
    @RequestMapping("/ajaxUserStudyRecord")
    public String ajaxUserStudyRecord(HttpServletRequest request,
                                      Model model,
                                      @RequestBody Map<String, Object> params,
                                      @RequestParam("currentPage") int currentPage) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId", userId);
        Map<String, Object> dataMap = courseLearnFlowService.findCoursePlayRecordFlowPage(params, currentPage, 10, true, false, true, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/course/play/record/ajax-front-studyRecord");
    }

    /**
     * 用户随堂练习结果
     *
     * @return
     */
    @RequestMapping("/userPracticeRecord")
    public String userPracticeRecord(Model model, @RequestParam BigDecimal examRecordId, @RequestParam BigDecimal courseId, @RequestParam BigDecimal catalogId, @RequestParam double scorePercent, @RequestParam boolean pass,
                                     @RequestParam BigDecimal byCourseId, HttpServletRequest request) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> condition = new HashMap<>();
        condition.put("id", examRecordId);
        condition.put("userId", userId);
        condition.put("loreSetPointData", true);
        condition.put("rightNumData", true);
        Map<String, Object> dataMap = loadStartExamService.loadExamRecord(condition);
        if (ResultUtil.getSuccess(dataMap)) {
            model.addAttribute("examRecord", ResultUtil.getEntity(dataMap));
            Map<String, Object> cardMap = loadStartExamService.loadExamRecordCard(userId, examRecordId, 0);
            model.addAttribute("cardMap", cardMap);
        }
        model.addAttribute("courseId", courseId);
        model.addAttribute("catalogId", catalogId);
        model.addAttribute("byCourseId", byCourseId);
        model.addAttribute("scorePercent", scorePercent);
        model.addAttribute("pass", pass);
        return resultPage("/course/play/record/front-practiceRecord");
    }

    /**
     * 查询今日是否有直播
     *
     * @param request
     * @return
     */
    @RequestMapping("/getToDayLive")
    @ResponseBody
    public Map<String, Object> getToDayLive(HttpServletRequest request) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        Integer count = courseCatalogService.queryCurrentCount(params);
        return ResultUtil.SUCCESS(count);
    }

    /**
     * 直播课程表
     *
     * @return
     */
    @RequestMapping("/userLiveSchedule")
    public String userLiveSchedule(HttpServletRequest request) {
        return resultPage("/course/play/record/front-liveSchedule");
    }

    /**
     * 直播课程表ajax追加
     *
     * @param model
     * @param request
     * @param params
     * @return
     */
    @RequestMapping("/ajax/userLiveSchedule")
    public String ajaxUserLiveSchedule(Model model, HttpServletRequest request, @RequestBody Map<String, Object> params) {
        int year = (int) params.get("year");
        int month = (int) params.get("month");
        int date = (int) params.get("date");
        Date now = DateUtils.parseDate(year + "-" + month + "-" + date, "yyyy-MM-dd");
        params.put("nowDate", now);
        params.put("status", 1);
        params.put("nodeType", 2);
        params.put("userId", webMobileLoginUtil.getLoginUserId(request));
        params.put("courseStatus", OrderStatus.SUCCESS);
        List<CourseCatalog> dataMap = courseCatalogService.findCourseCatalogPage(params);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/course/play/record/ajax-front-liveSchedule");
    }
}
